<?php

namespace model;

use common\Model;

class images_image extends Model
{
    const TABLE = "images_image";
    const TABLES = "images";
	private $DB;
    function __construct()
    {
        $this->DB = parent::db();
    }

    //查找一条数据
    public function findData()
    {
        $where['id'] = $_GET['id'];
        return $this->DB->table(self::TABLE)->where($where)->find();
    }

    //获取数据并分页
    public function jsonPageSelect()
    {
        $p = intval($_GET['page'] ?? 0) ?: 1;
        $num = intval($_GET['limit'] ?? 0) ?: 15;
        $where['aid'] = $_GET['aid'];

        $page = ['num' => $num, 'p' => $p, 'return' => true];
        return [
            'data' => $this->DB->table(self::TABLE)->where($where)->Page($page)->order('id DESC')->select(),
            'page' => $this->DB->getPage()
        ];
    }

    //查找一条所属图集的数据
    public function findImagesData()
    {
        $where['id'] = $_GET['aid'];
        return $this->DB->table(self::TABLES)->where($where)->find();
    }

    //添加数据
    public function insertData()
    {
        if (empty($_POST['imgsrc']))
            return ['status' => 0, 'msg' => '请填写必要数据'];
        $data = [
            'aid' => $_POST['aid'],
            'imgsrc' => VarFilter($_POST['imgsrc']),
            'info' => VarFilter($_POST['info']),
        ];
        $re_key = $this->DB->table(self::TABLE)->insert($data);
        if ($re_key) {
            SqlLog("添加图集图片信息ID：" . $re_key . "图集ID：" . $_POST['aid']);

            return ['status' => 1, 'msg' => '添加成功'];
        } else {
            return ['status' => 0, 'msg' => '数据错误！添加失败'];
        }
    }
    //更新数据
    public function updateData()
    {
        if (empty($_POST['imgsrc']))
            return ['status' => 0, 'msg' => '请填写必要数据'];
        $where['id'] = $_POST['id'];
        $data = [
            'imgsrc' => VarFilter($_POST['imgsrc']),
            'info' => VarFilter($_POST['info']),
        ];
        $re_key = $this->DB->table(self::TABLE)->Where($where)->Update($data);
        if ($re_key > 0) {
            SqlLog("修改图集图片信息ID：" . $_POST['id']);
            return ['status' => 1, 'msg' => '修改成功'];
        } else {
            return ['status' => 0, 'msg' => '数据没有变化'];
        }
    }

    //删除一条数据
    public function deleteOneData()
    {
        $where['id'] = $_POST['id'];
        SqlLog("删除图集图片信息ID：" . $_POST['id']);
        return $this->DB->table(self::TABLE)->where($where)->Delete();
    }



    
}
